package com.invite.me.beans.presenter;

import android.util.Log;


import com.google.gson.Gson;
import com.invite.me.api.UrlConstants;
import com.invite.me.beans.fabean.InviteRecordDetailByXqFaBean;
import com.invite.me.beans.fabean.NameattestationfaBean;
import com.invite.me.beans.res.InviteRecordDetailByXqShouBean;
import com.invite.me.beans.res.InviteRecordDetailZfbBeanShou;
import com.invite.me.beans.res.NameattestationBean;
import com.invite.me.ui.yaoyue.InviteRecordDetailActivity;
import com.invite.me.util.SspPrefUtils;
import com.invite.me.wlqq.Constants;
import com.invite.me.wlqq.Contract;
import com.invite.me.wlqq.RxJavaDataImpl;

import java.io.IOException;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class InviteRecordDetailPresenter implements Contract.BasePresenter{

    InviteRecordDetailActivity splashActivity;

    public InviteRecordDetailPresenter(InviteRecordDetailActivity signInActivity) {
        this.splashActivity = signInActivity;
        splashActivity.setPresenter(this);
    }

    @Override
    public void start() {

    }
    //查看详情页
    public void ChakanXiangqing(String aid ) {

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(Constants.HEADER_KEY, Constants.HEADER_VALUE);
        hashMap.put("userId", SspPrefUtils.getString(Constants.SSPTOKEN, ""));
        InviteRecordDetailByXqFaBean logeInDataBean = new InviteRecordDetailByXqFaBean();
        logeInDataBean.setId(aid);




        Gson gson = new Gson();
        String phoneNumber = gson.toJson(logeInDataBean);
        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);

        RxJavaDataImpl.postData(UrlConstants.CKYYJLXQ, hashMap, requestBody, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        InviteRecordDetailByXqShouBean gankInfo = gson.fromJson(str, InviteRecordDetailByXqShouBean.class);
                        splashActivity.onDataSucess(gankInfo);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("onerror", e + "");
                splashActivity.onDataFaile(e);
            }

            @Override
            public void onComplete() {
                splashActivity.onComplete();
            }
        });
    }
    //支付宝支付
    public void ZhiFuBaoZhiFu(String aid ) {

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(Constants.HEADER_KEY, Constants.HEADER_VALUE);
        hashMap.put("userId", SspPrefUtils.getString(Constants.SSPTOKEN, ""));
        InviteRecordDetailByXqFaBean logeInDataBean = new InviteRecordDetailByXqFaBean();
        logeInDataBean.setId(aid);
        Gson gson = new Gson();
        String phoneNumber = gson.toJson(logeInDataBean);
        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);

        RxJavaDataImpl.postData(UrlConstants.ZHIFU, hashMap, requestBody, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        String r= str.replace("\"", "");
                        InviteRecordDetailZfbBeanShou inviteRecordDetailZfbBeanShou=new InviteRecordDetailZfbBeanShou();
                        inviteRecordDetailZfbBeanShou.setZifuchuan(r);
//                        Gson gson = new Gson();
//                        InviteRecordDetailZfbBeanShou gankInfo = gson.fromJson(str, InviteRecordDetailZfbBeanShou.class);
                        splashActivity.onDataSucess(inviteRecordDetailZfbBeanShou);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("onerror", e + "");
                splashActivity.onDataFaile(e);
            }

            @Override
            public void onComplete() {
                splashActivity.onComplete();
            }
        });
    }


    //提交实名认证信息
    public void tijiaoshimingrenzheng(String name,String shenfen,String zhengmian,String fanmian){

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(Constants.HEADER_KEY, Constants.HEADER_VALUE);
        hashMap.put("userId", SspPrefUtils.getString(Constants.SSPTOKEN, ""));
        NameattestationfaBean logeInDataBean = new NameattestationfaBean();
        logeInDataBean.setName(name);
        logeInDataBean.setIdCard(shenfen);
        logeInDataBean.setIdCardFrontImg(zhengmian);
        logeInDataBean.setIdCardBackImg(fanmian);
        Gson gson = new Gson();
        String phoneNumber = gson.toJson(logeInDataBean);
        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);
        RxJavaDataImpl.postData(UrlConstants.API_TJSMRZ, hashMap, requestBody,new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        NameattestationBean gankInfo = gson.fromJson(str, NameattestationBean.class);
                        splashActivity.onDataSucess(gankInfo);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("onerror", e+"" );
                splashActivity.onDataFaile(e);
            }

            @Override
            public void onComplete() {
                splashActivity.onComplete();
            }
        });
    }



}
